<?php

ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\pockets\constInterface\BrandLevel;

$brandId = $argv[1];

$level = $argv[2];
if (!isset(BrandLevel::BRAND_LEVELS[$level])) {
    echo "level error!!!" . PHP_EOL;
    exit;
}

[$growthStart, $growthEnd] = BrandLevel::BRAND_LV_GROWTH_VALUE_RANGE[$level];

$growth = $argv[3] ?? $growthStart;

if (!($growth >= $growthStart && $growth <= $growthEnd)) {
    echo "growth not match level!!!" . PHP_EOL;
    exit;
}

$brandInfo = \Biz\model\brand\Brand::getInfo($brandId);

if (empty($brandInfo)) {
    echo "brand error!!!" . PHP_EOL;
    exit;
}

if ($brandInfo['level'] - $level != 1) {
    echo "level set error!!!" . PHP_EOL;
    exit;
}

if ($brandInfo['brand_type'] != \Biz\model\brand\Brand::BRAND_TYPE_EXPIRE) {
    echo "not expired brand!!!" . PHP_EOL;
    exit;
}

$lastLevel = $brandInfo['level'];

\Biz\model\brand\Brand::update(['level' => $level, 'growth_value' => $growth], ['id' => $brandId]);

$ret = \Biz\service\BrandRights\BrandLevelRightsService::afterExpiredBrandLevelDown($brandId, $level, $lastLevel);

var_dump($ret);

